﻿Public Class ListBoxTarea

    Private Sub ListBoxTarea_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me.ListBox1.Items
            .Add("Visual Foxpro")
            .Add("SQL Server 2008")
            .Add("Oralce")
            .Add("Sybase")
            .Add("C++")
            .Add("Java")
            .Add("C#")
            .Add("Pascal")
            .Add("Lisp")
            .Add("Prolog")
            .Add("Visual Basic.NET")
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.ListBox1.SelectedItems.Count > 0 Then
            copySelectedItems(Me.ListBox1, Me.ListBox2)
            removeSelectedItems(Me.ListBox1)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.ListBox2.SelectedItems.Count > 0 Then
            copySelectedItems(Me.ListBox2, Me.ListBox1)
            removeSelectedItems(Me.ListBox2)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        copyAllItems(Me.ListBox1, Me.ListBox2)
        removeAllItems(Me.ListBox1)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        copyAllItems(Me.ListBox2, Me.ListBox1)
        removeAllItems(Me.ListBox2)
    End Sub

#Region "Reusable section"

    Private Sub removeSelectedItems(ByRef listBox As ListBox)
        For i As Integer = 0 To listBox.SelectedItems.Count - 1
            listBox.Items.Remove(listBox.SelectedItem)
        Next
    End Sub

    Private Sub copySelectedItems(ByRef listBoxSource As ListBox, ByRef listBoxTarget As ListBox)
        For Each element As Object In listBoxSource.SelectedItems
            listBoxTarget.Items.Add(element.ToString)
        Next
    End Sub

    Private Sub removeAllItems(ByRef listBox As ListBox)
        listBox.Items.Clear()
    End Sub

    Private Sub copyAllItems(ByRef listBoxSource As ListBox, ByRef listBoxTarget As ListBox)
        For Each element As Object In listBoxSource.Items
            listBoxTarget.Items.Add(element.ToString)
        Next
    End Sub

#End Region

   
End Class